package com.cyss.greenball.view;

import com.cyss.greenball.exception.GameOverException;
import com.cyss.greenball.main.R;
import com.cyss.greenball.main.WorldActivity;
import com.cyss.greenball.util.ActivityUtil;
import com.cyss.greenball.util.SystemUtil;
import com.cyss.greenball.world.BallWorld;
import com.cyss.greenball.world.BallWorldFactory;
import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Toast;

public class WorldView extends SurfaceView implements Runnable, Callback {

	private SurfaceHolder holder;
	private Thread mainThread;
	private boolean runFlag = true;
	private BallWorld ballWorld;
	private Canvas canvas;

	private SensorManager sensorMgr;
	private Handler handler;
	private AbstractCommonData acdGame;

	private int nowMission = 0;

	public WorldView(Context context, Handler handler, int nowMission,
			AbstractCommonData acdGame) {
		super(context);
		this.handler = handler;
		this.nowMission = nowMission;
		this.acdGame = acdGame;
		this.setKeepScreenOn(true);
		this.setFocusable(true);
		holder = this.getHolder();
		holder.addCallback(this);
		if (this.acdGame == null)
			ballWorld = BallWorldFactory.createBallWorldWithTest();
		else
			ballWorld = BallWorldFactory
					.createBallWorldWithCommonDataAndFrame(this.acdGame);
		sensorMgr = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		SensorEventListener lsn = new SensorEventListener() {
			public void onSensorChanged(SensorEvent e) {
				float x = -e.values[SensorManager.DATA_X] * 5;
				float y = e.values[SensorManager.DATA_Y] * 5;
				// float z = e.values[SensorManager.DATA_Z];
				ballWorld.setGravity(x, y);
			}
			public void onAccuracyChanged(Sensor s, int accuracy) {
			}
		};
		sensorMgr
				.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mainThread = new Thread(this);
		mainThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		runFlag = false;
	}

	@Override
	public void run() {
		while (runFlag) {
			try {
				canvas = holder.lockCanvas();
				canvas.drawColor(Color.GRAY);
				ballWorld.DrawAllElement(canvas);
			} catch (GameOverException goe) {
				goe.printStackTrace();
				final Context context = this.getContext();
				AbstractCommonData acd = DataConvertFactory.getInstance();
				AbstractCommonData head = acd.getDataValue("head");
				head.putIntValue("activity_function", ActivityUtil.DIALOG);
				acd.putStringValue("title",
						context.getString(R.string.game_tip));
				if (goe.getGameOverFlag() == SystemUtil.GAME_STATE_SUCCESS) {
					acd.putStringValue("text",
							context.getString(R.string.through_game));
					acd.putStringValue("button1",
							context.getString(R.string.next_mission));
					acd.putStringValue("button2",
							context.getString(R.string.quit_game));
					acd.putObjectValue("button1_listener",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							});
					acd.putObjectValue("button2_listener",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									AbstractCommonData finshActivity = DataConvertFactory
											.getInstance();
									finshActivity
											.getDataValue("head")
											.putIntValue(
													"activity_mode",
													ActivityUtil.ACTIVITY_MODE_FINISH);
									Message msg = WorldView.this.handler
											.obtainMessage();
									msg.obj = finshActivity;
									handler.sendMessage(msg);
								}
							});
				} else if (goe.getGameOverFlag() == SystemUtil.GAME_STATE_FAIL) {
					acd.putStringValue("text",
							this.getContext().getString(R.string.game_fail));
					acd.putStringValue("button1",
							this.getContext().getString(R.string.try_again));
					acd.putStringValue("button2",
							this.getContext().getString(R.string.quit_game));
					acd.putObjectValue("button1_listener",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									AbstractCommonData finshActivity = DataConvertFactory
											.getInstance();
									finshActivity
											.getDataValue("head")
											.putIntValue(
													"activity_mode",
													ActivityUtil.ACTIVITY_MODE_RESUME);
									Message msg = WorldView.this.handler
											.obtainMessage();
									msg.obj = finshActivity;
									handler.sendMessage(msg);
								}
							});
					acd.putObjectValue("button2_listener",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									AbstractCommonData finshActivity = DataConvertFactory
											.getInstance();
									finshActivity
											.getDataValue("head")
											.putIntValue(
													"activity_mode",
													ActivityUtil.ACTIVITY_MODE_FINISH);
									Message msg = WorldView.this.handler
											.obtainMessage();
									msg.obj = finshActivity;
									WorldView.this.handler.sendMessage(msg);
									dialog.dismiss();
								}
							});
				} else {

				}
				Message msg = this.handler.obtainMessage();
				msg.obj = acd;
				handler.sendMessage(msg);
				runFlag = false;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					holder.unlockCanvasAndPost(canvas);
				} catch (Exception e) {
				}
			}
			try {
				Thread.sleep((int) SystemUtil.TIMESTEP * 1000);
			} catch (Exception ex) {
			}
		}
	}
}
